#include <bits/stdc++.h>

using namespace std;

int f[15][15][3];
int val[15];

int calc(int digits,int mod,int type,bool lim){
    if(digits<=0) {if(mod==0&&type==2) return 1;else return 0;}
    if((!lim)&&f[digits][mod][type]!=-1) return f[digits][mod][type];

    int mx=lim?val[digits]:9;
    int ans=0;
    for(int i=0;i<=mx;i++){
        int nmod=(mod*10+i)%13;
        int ntyp;
        if(type==2) ntyp=2;
        else if(i==1) ntyp=1;
        else if(type==1) ntyp=i==3?2:0;
        else ntyp=0;

        ans+=calc(digits-1,nmod,ntyp,lim&&i==mx);
    }

    if(!lim) f[digits][mod][type]=ans;
    return ans;
}

int main(){
    int n;

    memset(f,-1,sizeof f);
    while(~scanf("%d",&n)){
        int len=0;
        while(n){
            val[++len]=n%10;
            n/=10;
        }
        printf("%d\n",calc(len,0,0,1));
    }
}
